package cn.com.blueInfo.autoCode.database;

import cn.com.blueInfo.bpm.person.enums.*;
import cn.com.blueInfo.bpm.person.request.UserAccountDTO;
import cn.com.blueInfo.bpm.person.request.UserBaseDTO;
import cn.com.blueInfo.bpm.person.response.UserAccountVO;
import cn.com.blueInfo.bpm.person.response.UserBaseVO;
import cn.com.blueInfo.bpm.person.service.UserAccountService;
import cn.com.blueInfo.bpm.person.service.UserBaseService;
import cn.com.blueInfo.bpm.system.request.ApplicationDTO;
import cn.com.blueInfo.bpm.system.request.TenantDTO;
import cn.com.blueInfo.bpm.system.response.ApplicationVO;
import cn.com.blueInfo.bpm.system.response.TenantVO;
import cn.com.blueInfo.bpm.system.service.ApplicationService;
import cn.com.blueInfo.bpm.system.service.TenantService;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建超管数据
 */
@Component
public class CreateSupperData {

    @Autowired
    private TenantService tenantService;

    @Autowired
    private ApplicationService applicationService;

    @Autowired
    private UserBaseService userBaseService;

    @Autowired
    private UserAccountService userAccountService;

    @Autowired
    private CreateMenuDefaultData createMenuDefaultData;

    public void handler() {
        Long tenantId = addTenantData(addTenantData());
        Long applicationId = addApplicationData(addApplicationData(tenantId));
        Long userBaseId = addUserBaseData(addUserBaseData(tenantId, applicationId));
        Long userAccountId = addUserAccountData(addUserAccountData(userBaseId, tenantId, applicationId));

        String tenantUpdateResult = updateTenantCreateUserId(tenantId, userAccountId);
        String applicationUpdateResult = updateApplicationCreateUserId(applicationId, userAccountId);
        String userBaseUpdateResult = updateUserBaseCreateUserId(userBaseId, userAccountId);
        String userAccountUpdateResult = updateUserAccountCreateUserId(userAccountId, userAccountId);
        System.out.println(tenantUpdateResult);
        System.out.println(applicationUpdateResult);
        System.out.println(userBaseUpdateResult);
        System.out.println(userAccountUpdateResult);

        Map<String, Long> commonData = new HashMap<>();
        commonData.put("tenantId", tenantId);
        commonData.put("applicationId", applicationId);
        commonData.put("createUserId", userAccountId);

        createMenuDefaultData.handler(commonData);

    }

    // =================================平台超管账号信息数据================================
    private String updateUserAccountCreateUserId(Long userAccountId, Long createUserId) {
        UserAccountDTO.UpdatePartialDTO userAccountUpdatePartialDTO = new UserAccountDTO.UpdatePartialDTO();
        userAccountUpdatePartialDTO.setId(userAccountId);
        userAccountUpdatePartialDTO.setCreateUserId(createUserId);
        ResultInfo<Integer> resultUpdateInfo = userAccountService.modifyUserAccountPartial(userAccountUpdatePartialDTO);
        return resultUpdateInfo.getMessage();
    }

    private Long addUserAccountData(UserAccountDTO userAccountDTO) {
        long resultUserAccountId = 0L;
        ResultInfo<Integer> resultAddUserAccount = userAccountService.addUserAccount(userAccountDTO);
        if (resultAddUserAccount.isResult()) {
            UserAccountDTO.QueryDTO userAccountQueryDTO = new UserAccountDTO.QueryDTO();
            userAccountQueryDTO.setLoginName("administrator");
            ResultInfo<Page<UserAccountVO>> resultUserAccount = userAccountService.queryUserAccountPage(userAccountQueryDTO);
            Page<UserAccountVO> userAccountVOPage = resultUserAccount.getData();
            List<UserAccountVO> userAccountVOList = userAccountVOPage.getRecords();
            resultUserAccountId = Long.parseLong(userAccountVOList.get(0).getId());
        }
        return resultUserAccountId;
    }

    private UserAccountDTO addUserAccountData(Long userBaseId, Long tenantId, Long applicationId) {
        UserAccountDTO userAccountDTO = new UserAccountDTO();
        userAccountDTO.setUserBaseId(userBaseId);
        userAccountDTO.setTenantId(tenantId);
        userAccountDTO.setApplicationId(applicationId);
        userAccountDTO.setLoginName("administrator");
        userAccountDTO.setAccountStatus(UserAccountStatus.NORMAL);
        userAccountDTO.setPassword("MyMemory19910516");
        return userAccountDTO;
    }

    // =================================平台超管基本信息数据================================
    private String updateUserBaseCreateUserId(Long userBaseId, Long createUserId) {
        UserBaseDTO.UpdatePartialDTO userBaseUpdatePartialDTO = new UserBaseDTO.UpdatePartialDTO();
        userBaseUpdatePartialDTO.setId(userBaseId);
        userBaseUpdatePartialDTO.setCreateUserId(createUserId);
        ResultInfo<Integer> resultUpdateInfo = userBaseService.modifyUserBasePartial(userBaseUpdatePartialDTO);
        return resultUpdateInfo.getMessage();
    }

    private Long addUserBaseData(UserBaseDTO userBaseDTO) {
        long resultUserBaseId = 0L;
        ResultInfo<Integer> resultAddUserBase = userBaseService.addUserBase(userBaseDTO);
        if (resultAddUserBase.isResult()) {
            UserBaseDTO.QueryDTO userBaseQueryDTO = new UserBaseDTO.QueryDTO();
            userBaseQueryDTO.setUserNum(68);
            ResultInfo<Page<UserBaseVO>> resultUserBase = userBaseService.queryUserBasePage(userBaseQueryDTO);
            Page<UserBaseVO> userBaseVOPage = resultUserBase.getData();
            List<UserBaseVO> userBaseVOList = userBaseVOPage.getRecords();
            resultUserBaseId = Long.parseLong(userBaseVOList.get(0).getId());
        }
        return resultUserBaseId;
    }

    private UserBaseDTO addUserBaseData(Long tenantId, Long applicationId) {
        UserBaseDTO userBaseDTO = new UserBaseDTO();
        userBaseDTO.setIdentityNo("120105199106072412");
        userBaseDTO.setUserNum(68);
        userBaseDTO.setUserStatus(UserBaseUserStatus.ON_THE_JOB);
        userBaseDTO.setIdentityType(UserBaseIdentityType.ID_CARD);
        userBaseDTO.setEntryDate(LocalDate.of(2016, 6, 1));
        userBaseDTO.setEmploymentType(UserBaseEmploymentType.REGULAR_EMPLOYEE);
        userBaseDTO.setContractStartDate(LocalDate.of(2016, 6, 1));
        userBaseDTO.setSex(UserBaseSex.MALE);
        userBaseDTO.setMobile("17695539308");
        userBaseDTO.setContractEndDate(LocalDate.of(9999, 12, 31));
        userBaseDTO.setUserName("平台超级管理员");
        userBaseDTO.setUserCode("SUPPER-0068");
        userBaseDTO.setTenantId(tenantId);
        userBaseDTO.setApplicationId(applicationId);
        return userBaseDTO;
    }

    // =================================租户系统数据================================
    private String updateApplicationCreateUserId(Long applicationId, Long createUserId) {
        ApplicationDTO.UpdatePartialDTO applicationUpdatePartialDTO = new ApplicationDTO.UpdatePartialDTO();
        applicationUpdatePartialDTO.setId(applicationId);
        applicationUpdatePartialDTO.setCreateUserId(createUserId);
        ResultInfo<Integer> resultUpdateInfo = applicationService.modifyApplicationPartial(applicationUpdatePartialDTO);
        return resultUpdateInfo.getMessage();
    }

    private Long addApplicationData(ApplicationDTO applicationDTO) {
        long resultApplicationId = 0L;
        ResultInfo<Integer> resultAddApp = applicationService.addApplication(applicationDTO);
        if (resultAddApp.isResult()) {
            ApplicationDTO.QueryDTO applicationQueryDTO = new ApplicationDTO.QueryDTO();
            applicationQueryDTO.setAppCode("SUPPER_APP");
            ResultInfo<Page<ApplicationVO>> resultApp = applicationService.queryApplicationPage(applicationQueryDTO);
            Page<ApplicationVO> applicationVOPage = resultApp.getData();
            List<ApplicationVO> applicationVOList = applicationVOPage.getRecords();
            resultApplicationId = Long.parseLong(applicationVOList.get(0).getId());
        }
        return resultApplicationId;
    }

    private ApplicationDTO addApplicationData(Long tenantId) {
        ApplicationDTO applicationDTO = new ApplicationDTO();
        applicationDTO.setAppCode("SUPPER_APP");
        applicationDTO.setAppName("系统超级管理员");
        applicationDTO.setDescription("仅供创建租户/租户系统/租户管理员");
        applicationDTO.setTenantId(tenantId);
        return applicationDTO;
    }

    // =================================租户数据================================
    private String updateTenantCreateUserId(Long tenantId, Long createUserId) {
        TenantDTO.UpdatePartialDTO tenantUpdatePartialDTO = new TenantDTO.UpdatePartialDTO();
        tenantUpdatePartialDTO.setId(tenantId);
        tenantUpdatePartialDTO.setCreateUserId(createUserId);
        ResultInfo<Integer> resultUpdateInfo = tenantService.modifyTenantPartial(tenantUpdatePartialDTO);
        return resultUpdateInfo.getMessage();
    }

    private Long addTenantData(TenantDTO tenantDTO) {
        long resultTenantId = 0L;
        ResultInfo<Integer> resultAddTenant = tenantService.addTenant(tenantDTO);
        if (resultAddTenant.isResult()) {
            TenantDTO.QueryDTO tenantQueryDTO = new TenantDTO.QueryDTO();
            tenantQueryDTO.setTenantCode("SUPER");
            ResultInfo<Page<TenantVO>> resultTenant = tenantService.queryTenantPage(tenantQueryDTO);
            Page<TenantVO> tenantVOPage = resultTenant.getData();
            List<TenantVO> tenantVOList = tenantVOPage.getRecords();
            resultTenantId = Long.parseLong(tenantVOList.get(0).getId());
        }
        return resultTenantId;
    }

    private TenantDTO addTenantData() {
        TenantDTO tenantDTO = new TenantDTO();
        tenantDTO.setTenantCode("SUPER");
        tenantDTO.setTenantName("超级管理员");
        tenantDTO.setContactUser("苏希辰");
        tenantDTO.setContactMobile("13652101420");
        return tenantDTO;
    }

}
